<template>
	<view class="coupon">
		<!-- 标签页区域 -->
		<view class="tabs" :class="{ loading: !tabDataLoaded }">
			<view v-for="(item,index) in tabsList" :key="index" 
				class="tabItem" 
				@click="changeTabs(index)" 
				:class="{ active: tabsCurrent === index }"
				:style="{borderBottom: tabsCurrent == index?'#73F0EA 8rpx solid':''}">
				<view class="size"
					:style="{
						color: tabsCurrent == index ?'#73F0EA':'#999999',
						fontWeight: tabsCurrent == index ?'800':'400'
					}">
					{{item.name}}
				</view>
			</view>
		</view>
		
		<!-- 内容区域 -->
		<scroll-view scroll-y="true" class="scroll_box" @scrolltolower="scrolltolower">
			<!-- 加载状态 -->
			<view v-if="loading" class="loading-container">
				<view class="loading-card" v-for="n in 3" :key="n" :style="{ animationDelay: (n-1) * 0.2 + 's' }">
					<view class="skeleton-coupon"></view>
				</view>
			</view>
			
			<!-- 错误状态 -->
			<view v-else-if="loadError" class="error-container">
				<view class="error-content">
					<u-icon name="wifi-off" size="60" color="#ccc"></u-icon>
					<text class="error-text">加载失败，请重试</text>
					<u-button type="primary" size="medium" @click="retryLoad">重新加载</u-button>
				</view>
			</view>
			
			<!-- 优惠券列表 -->
			<view v-else-if="couponList.length" class="comlum_box">
				<view v-for="(item,index) in couponList" :key="index" 
					class="item" 
					:style="{
						animationDelay: index * 0.1 + 's',
						opacity: !tabsCurrent ?'1':'0.6'
					}">
					<view class="header" @click="changeCouponActive(index)">
						<!-- 满减 -->
						<view class="left">
							<view class="minus">
								<view class="unit">
									￥
								</view>
								<view class="number">
									{{item.face}}
								</view>
							</view>
							<view class="full">
								{{item.threshold}}
							</view>
						</view>
						<!-- 虚线 -->
						<view class="height-dotted-line">
						</view>
						<!-- 名称 -->
						<view class="right">
							<view class="name-of-shop">
								<view class="title">
									{{item.name}}
								</view>
								<view class="time">
									使用期限：{{item.in_date}}
								</view>
							</view>
							<image :src="tabsCurrent == 1 ?$IMG_URL('/static/user/used.png'):tabsCurrent == 2 ?$IMG_URL('/static/user/past_due.png'):''" mode="" class="img"></image>
						</view>
					</view>
					<!-- 虚线 -->
					<view class="width-dotted-line">
					</view>
					<view class="Folding-panel">
						<view class="row-box" @click="toggle(index)">
							<view class="official">
								{{item.top_ast}}
							</view>
							<u-icon :name="item.open?'arrow-up':'arrow-down'" size="14"
								color="#dbdbdb"></u-icon>
						</view>
						<view class="remark" v-if="item.open">
							<view v-for="(childitem,childindex) in item.ast_list" :key="childindex">
								{{childitem}}
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" />
			</view>
			<empty v-else :content="getEmptyText()"></empty>
			
		</scroll-view>
	</view>
</template>

<script>
	import{
		coupon
	} from '@/api/user.js'
	import noCentent from '@/pages/index/components/no_content.vue'
	import empty from './components/empty.vue'
	export default{
		components:{
			noCentent,
			empty
		},
		data() {
			return {
				// 查询参数
				intent: {
					form: 'usable',
					page: 1
				},
				// 加载更多状态
				status: 'loadmore',
				// 标签页配置
				tabsList: [
					{ name: '可使用' },
					{ name: '已使用' },
					{ name: '已过期' }
				],
				// 当前选中的标签页索引
				tabsCurrent: 0,
				// 优惠券列表数据
				couponList: [],
				// 页面状态管理
				loading: false,
				loadError: false,
				// 标签页数据加载状态
				tabDataLoaded: true
			}
		},
		computed: {
			/**
			 * 获取空状态显示文本
			 */
			getEmptyText() {
				const emptyTexts = [
					'可使用优惠券',
					'已使用优惠券',
					'已过期优惠券'
				]
				return () => emptyTexts[this.tabsCurrent] || emptyTexts[0]
			}
		},
		onLoad() {
			this.initializePageData()
		},
		methods: {
			/**
			 * 初始化页面数据
			 */
			async initializePageData() {
				try {
					this.loading = true
					this.loadError = false
					
					await this.getCouponData()
				} catch (error) {
					console.error('页面初始化失败:', error)
					this.loadError = true
					uni.showToast({
						title: '加载失败，请重试',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			/**
			 * 获取优惠券数据
			 */
			async getCouponData() {
				try {
					const { data: res } = await coupon(this.intent)
					if (res.code === 1) {
						this.status = 'loading'
						
						if (this.intent.page > res.data.all_Page) {
							this.status = 'nomore'
							return
						}
						
						this.status = 'more'
						this.couponList = this.couponList.concat(res.data.coupon_list || [])
					} else {
						throw new Error(res.msg || '获取优惠券列表失败')
					}
				} catch (error) {
					console.error('获取优惠券失败:', error)
					uni.showToast({
						title: '获取优惠券列表失败',
						icon: 'none'
					})
					throw error
				}
			},
			
			/**
			 * 切换标签页
			 * @param {number} index - 标签页索引
			 */
			changeTabs(index) {
				if (this.tabsCurrent === index || this.loading) return
				
				this.status = 'more'
				this.intent.page = 1
				this.couponList = []
				
				if (!index) {
					this.intent.form = 'usable'
				} else if (index === 1) {
					this.intent.form = 'used'
				} else {
					this.intent.form = 'past'
				}
				
				this.tabsCurrent = index
				this.getCouponData()
			},
			
			/**
			 * 切换优惠券激活状态
			 * @param {number} index - 优惠券索引
			 */
			changeCouponActive(index) {
				// 可以在这里添加优惠券激活逻辑
				console.log('优惠券点击:', index)
			},
			
			/**
			 * 切换折叠面板状态
			 * @param {number} index - 优惠券索引
			 */
			toggle(index) {
				this.$set(this.couponList[index], 'open', !this.couponList[index].open)
			},
			
			/**
			 * 滚动到底部加载更多
			 */
			scrolltolower() {
				if (this.status === 'nomore' || this.loading) return
				
				this.intent.page++
				this.getCouponData()
			},
			
			/**
			 * 重新加载页面数据
			 */
			retryLoad() {
				this.initializePageData()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #e9e9e9;
		// 全局关闭点击高亮效果
		-webkit-tap-highlight-color: transparent;
		tap-highlight-color: transparent;
	}
	
	// 全局关闭所有可点击元素的高亮效果
	* {
		-webkit-tap-highlight-color: transparent;
		tap-highlight-color: transparent;
		outline: none;
		-webkit-touch-callout: none;
	}
	
	.coupon {
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.tabs {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-around;
			background-color: #ffffff;
			padding-top: 33rpx;
			transition: all 0.3s ease;
			
			&.loading {
				opacity: 0.7;
			}
			
			.tabItem {
				display: flex;
				flex-direction: column;
				align-items: center;
				height: 60rpx;
				position: relative;
				transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
				cursor: pointer;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				&.active {
					transform: translateY(-2rpx);
				}
				
				&:hover {
					transform: translateY(-1rpx);
				}
				
				&:active {
					background-color: transparent !important;
					box-shadow: none !important;
				}
				
				.size {
					font-size: 28rpx;
					letter-spacing: 0.62rpx;
					font-family: PingFang SC, PingFang SC-Heavy;
					transition: all 0.3s ease;
				}
			}
		}
	}
	
	.scroll_box {
		height: 90vh;
		width: 100%;
		margin-top: 24rpx;
		
		// 加载状态
		.loading-container {
			padding: 40rpx 24rpx;
			display: flex;
			flex-direction: column;
			gap: 20rpx;
			
			.loading-card {
				width: 100%;
				height: 280rpx;
				background: #ffffff;
				border-radius: 16rpx;
				opacity: 0;
				animation: pulse 1.5s ease-in-out infinite;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				.skeleton-coupon {
					width: 100%;
					height: 100%;
					background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
					background-size: 200% 100%;
					border-radius: 16rpx;
					animation: skeleton-loading 2s infinite;
				}
			}
		}
		
		// 错误状态
		.error-container {
			padding: 100rpx 24rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			// 关闭点击高亮效果
			-webkit-tap-highlight-color: transparent;
			tap-highlight-color: transparent;
			outline: none;
			-webkit-touch-callout: none;
			-webkit-user-select: none;
			user-select: none;
			
			.error-content {
				display: flex;
				flex-direction: column;
				align-items: center;
				gap: 20rpx;
				
				.error-text {
					font-size: 28rpx;
					color: #999;
					margin: 20rpx 0;
				}
			}
		}
		
		.comlum_box {
			width: 100%;
			padding: 0 24rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			
			.item {
				width: 100%;
				background: #ffffff;
				border-radius: 16rpx;
				display: flex;
				flex-direction: column;
				margin-bottom: 20rpx;
				z-index: 22;
				// 动画效果
				opacity: 0;
				transform: translateY(30rpx);
				animation: fadeInUp 0.6s ease forwards;
				transition: transform 0.3s ease;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				&:hover {
					transform: translateY(-5rpx);
				}
				
				&:active {
					transform: scale(0.98);
					background-color: transparent !important;
					box-shadow: none !important;
				}
		
				.header {
					width: 100%;
					height: 158rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
		
					.left {
						width: 185rpx;
						display: flex;
						flex-direction: column;
						align-items: center;
						padding-left: 32rpx;
		
						.minus {
							display: flex;
							flex-direction: row;
							align-items: baseline;
							margin-bottom: 2rpx;
		
							.unit {
								font-size: 24rpx;
								font-family: PingFang SC, PingFang SC-Bold;
								font-weight: 700;
								text-align: left;
								color: #f37364;
							}
		
							.number {
								font-size: 45rpx;
								font-family: DIN, DIN-Bold;
								font-weight: 700;
								text-align: left;
								color: #f37364;
							}
						}
		
						.full {
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC-Medium;
							font-weight: 500;
							text-align: center;
							color: #666666;
						}
					}
		
					.height-dotted-line {
						width: 0rpx;
						height: 118rpx;
						opacity: 0.1;
						border: 1rpx dashed #333333;
						margin-right: 33rpx;
					}
		
					.right {
						flex: 1;
						padding-right: 32rpx;
						display: flex;
						flex-direction: row;
						align-items: center;
		
						.name-of-shop {
							display: flex;
							flex-direction: column;
							width: 336rpx;
		
							.title {
								font-size: 32rpx;
								font-family: PingFang SC, PingFang SC-Bold;
								font-weight: 700;
								text-align: left;
								color: #333333;
								margin-bottom: 8rpx;
							}
		
							.time {
								font-size: 20rpx;
								font-family: PingFang SC, PingFang SC-Regular;
								font-weight: 400;
								text-align: left;
								color: #666666;
							}
						}
		
						.img{
							width: 116rpx;
							height: 116rpx;
						}
					}
				}
		
				// 虚线
				.width-dotted-line {
					opacity: 0.1;
					border: 1rpx dashed #333333;
				}
		
				// 折叠面板
				.Folding-panel {
					display: flex;
					flex-direction: column;
					width: 100%;
					padding: 18rpx 32rpx 16rpx;
		
					.row-box {
						width: 100%;
						display: flex;
						flex-direction: row;
						align-items: center;
						padding-bottom: 12rpx;
		
						.official {
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC-Regular;
							font-weight: 400;
							text-align: left;
							color: #666666;
							flex: 1;
						}
					}
		
					.remark {
						display: flex;
						flex-direction: column;
						width: 578rpx;
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC-Regular;
						font-weight: 400;
						text-align: left;
						color: #666666;
						line-height: 28rpx;
					}
				}
			}
		}
	}
	
	// 动画关键帧
	@keyframes fadeInUp {
		0% {
			opacity: 0;
			transform: translateY(30rpx);
		}
		100% {
			opacity: 1;
			transform: translateY(0);
		}
	}
	
	@keyframes pulse {
		0%, 100% {
			opacity: 0.4;
		}
		50% {
			opacity: 0.8;
		}
	}
	
	@keyframes skeleton-loading {
		0% {
			background-position: -200% 0;
		}
		100% {
			background-position: 200% 0;
		}
	}
	
	// 流光效果
	@keyframes shimmer {
		0% {
			background-position: -100% 0;
		}
		100% {
			background-position: 100% 0;
		}
	}
</style>